/* Thanks to the folks at http://www.jera.com/techinfo/jtns/jtn002.html for
 * this extremely simply but highly useful unit testing suite.  This code was
 * given with an unrestricted licence.
 *
 * This header file also incorporates code from Zed A. Shaw's 'Learn C the Hard
 * Way' tutorial series at http://c.learncodethehardway.org/book/ex20.html.
 */

#ifndef __min_test_h__
#define __min_test_h__

#include <assert.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>

#define mu_clean_errno() (errno == 0 ? "None" : strerror(errno))
#define mu_log_err(M, ...) fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, mu_clean_errno(), ##__VA_ARGS__)
#define mu_assert(T, M) return ((T) ? M : 0)
#define mu_abort(T, M, ...) do { if(!(T)) { mu_log_err(M, ##__VA_ARGS__); assert(T); } } while(0)
#define mu_run_test(T) do { char *M = T();  \
                            mu_tests_run++; \
                            if(M) return M; \
                          } while(0)

typedef char *(*testFunc)();

extern int mu_tests_run;

int mu_run_all_tests(testFunc tests[], size_t len)
{
    int i;
    char *res;
    int num_errs = 0;

    for(i=0; i<len; i++) {
        res = tests[i]();
        mu_tests_run++;
        if(res != 0) {
            num_errs++;
            printf("%s\n", res);
        }
    }

    return num_errs;
}
#endif
